<template>
  <div id="JudgeManagements">
    <!-- <BreadCrumb /> -->
    <!-- <competition-nav /> -->
    <!-- 主要区域 -->
    <div class="major-areas">
      <div class="jx-content" id="scrollcontent">
        <!-- 列表顶部功能按钮区 -->
        <div>
          <!-- 左侧 -->
          <div class="fl courseName">
            <div class="txt">评委信息：</div>
            <el-input v-model="pageInfo.info" placeholder="请输入姓名"></el-input>
            <div class="search-button" @click="searchJudgeInfo">搜索</div>
            <el-switch v-model="value1" inactive-text="关闭审批" @change="changeApproval"></el-switch>
          </div>
          <!-- 右侧 -->
          <div class="btn-group fr">
            <el-form :inline="true">
              <el-form-item>
                <div class="button-style" @click="add">&emsp;新 增&emsp;</div>
              </el-form-item>
              <el-form-item>
                <div class="button-style" @click="deleteFn">批量删除</div>
              </el-form-item>
              <el-form-item>
                <div class="button-style" @click="impotInfo"> 批量导入 </div>
              </el-form-item>
            </el-form>
          </div>
        </div>
        <!-- 任务列表 tableData-->
        <el-table class="chapter-info-table" :data="tableData" style="width: 100%" tooltip-effect="dark" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="name" label="评委姓名" width="100"></el-table-column>
          <el-table-column prop="userNumber" label="账号" width="100"></el-table-column>
          <el-table-column prop="telephoneNumber" label="手机号" width="150"></el-table-column>
          <el-table-column prop="position" label="职位" width="100"></el-table-column>
          <el-table-column prop="task" label="审批任务" width="300">
            <template slot-scope="{row}">
              <div v-if="row.task.length<5" class="table-task table-task-center">
                <div class="table-task-item" v-for="(item,index) in row.task" :key="index">{{item}}</div>
              </div>
              <div v-else class="table-task">
                <div class="table-task-item" v-for="(item,index) in row.task" :key="index">{{item}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="开通日期" width="200"></el-table-column>
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="initCode(scope.row.userId)">初始密码</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页查询 -->
        <el-pagination v-if="tableData.length" @current-change="handleCurrentChange" :current-page="currentPage" background layout="prev, pager, next" :total="totalCount"></el-pagination>
        <!-- 新增 -->
        <el-dialog :close-on-click-modal="false" title="添加评委" :visible.sync="dialog" width="600px" @close="close" id="addjudge">
          <el-form ref="form" label-width="100px" :model="form">
            <el-form-item label="姓名：" prop="name">
              <el-input class="no-ponstion" v-model="form.name" maxlength="20" show-word-limit placeholder="请输入姓名"></el-input>
            </el-form-item>
            <el-form-item label="手机号：" prop="telephoneNumber">
              <el-input class="no-ponstion" v-model="form.telephoneNumber" maxlength="11" show-word-limit placeholder="请输入手机号"></el-input>
            </el-form-item>
            <el-form-item label="职位：" prop="position">
              <el-input class="no-ponstion" type="text" v-model="form.position" maxlength="15" show-word-limit placeholder="请输入职位"></el-input>
            </el-form-item>
            <el-form-item label="审批任务">
              <el-select v-model="form.taskId" multiple placeholder="请选择任务">
                <el-option v-for="item in options" :key="item.value" :label="item.taskName" :value="item.taskId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div class="footer" style="text-align: center">
            <button class="button-style saveBtn" @click="onSubmit()">
              确 认
            </button>
          </div>
        </el-dialog>
        <!-- 导入 -->
        <el-dialog :close-on-click-modal="false" title="导入" class="dialog-excel" :visible.sync="dialogable" width="600px">
          <div class="dialog-content">
            <div class="excel-btn-box">
              <el-button class="button-style">上传Excel<input id="excel-file" type="file" multiple="multiple" auto-complete="off" @change="GetFile"></input></el-button>
              <!-- <a href="/tes/user/model/download/student"><span>学生信息模板.xls <i class="iconfont icon-tubiaozhizuo-"></i></span></a> -->
              <a href=" /tes/tempfiles/评委导入模板.xlsx">评委信息模板.xls <i class="iconfont icon-tubiaozhizuo-"></i></a>
            </div>
            <p v-if="dialogExcelFile">{{ dialogExcelFile }} <i class="iconfont icon-shanchu" @click="ExcelDelete"></i></p>
          </div>
          <span slot="footer" style="text-align:center">
            <el-button class="button-style" @click="ExcelSave">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import BreadCrumb from '@/components/BreadCrumb.vue'
import competitionNav from '@/views/competitionManagement/components/competitionNav.vue'
import {
  addTheJudges,
  deleteTheJudges,
  importExcelTheJudges,
  theJudgesListByPage,
  theTaskList,
  initPassWord,
  examinationApprovalStatus,
  getReportTypeTSCP,
} from '@/api/competitionPlatform'
export default {
  name: 'JudgeManagements',
  components: {
    BreadCrumb,
    competitionNav,
  },
  data() {
    return {
      // 课程id
      courseId: 0,
      //新增表单
      form: {
        courseId: 0,
        name: '',
        position: '',
        taskId: [],
        telephoneNumber: '',
        userNumber: '',
      },
      //审批任务选项
      options: [],
      // 渲染时的评委列表
      tableData: [],
      //分页查询
      pageInfo: {
        //评委信息搜索文本
        info: '',
        pageNumber: 1,
        pageSize: 10,
      },
      currentPage: 1,
      totalCount: 0,
      //审批开关
      value1: 0,
      // 新增评委弹窗开关
      dialog: false,
      //新增评委验证规则
      rules: [],
      // 批量导入弹窗开关
      dialogable: false,
      //批量导入时可选择的评委
      judgesList: [],
      // 要删除的评委编号列表
      deletJudgeList: [],
      //上传文件名字
      dialogExcelFile: '',
    }
  },
  created() {
    this.courseId = this.$localstorage.getLocalStorage('setCourseId')
    this.form.courseId = this.courseId
    //获取评委列表
    this.getJudgesListByPage()
    //获取任务列表
    this.getTaskList()
    //获取审批开关状态
    this.getApprovalType()
  },
  methods: {
    //分页查询
    handleCurrentChange(currentPage) {
      this.pageInfo.pageNumber = currentPage
      this.currentPage = currentPage

      //获取评委列表
      this.getJudgesListByPage()
    },
    //获取评委列表
    getJudgesListByPage() {
      //评委列表
      theJudgesListByPage(this.courseId, this.pageInfo)
        .then((res) => {
          if (res.code == 200) {
            this.tableData = res.data.records
            this.totalCount = res.data.total
          }
        })
        .catch((err) => {
          this.$message.error(res.message)
        })
    },
    //获取任务列表
    getTaskList() {
      //任务列表
      theTaskList(this.courseId)
        .then((res) => {
          if (res.code == 200) {
            this.options = res.data
          }
        })
        .catch((err) => {
          this.$message.error(res.message)
        })
    },
    //新增评委
    add() {
      this.dialog = true
      this.form.name = ''
      this.form.position = ''
      this.form.taskId = []
      this.form.telephoneNumber = ''
      this.form.userNumber = ''
    },
    //导入评委
    impotInfo() {
      this.dialogable = true
      this.filterText = ''
    },
    //选择要删除的评委
    handleSelectionChange(val) {
      let valArr = []
      for (let i = 0; i < val.length; i++) {
        valArr.push(val[i].userId)
      }
      this.deletJudgeList = valArr
    },
    // 批量删除
    deleteFn() {
      if (this.deletJudgeList.length == 0) {
        this.$message({
          message: '请选择要删除的评委',
          type: 'warning',
        })
        return
      }
      deleteTheJudges(this.courseId, this.deletJudgeList).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
          })
          //恢复删除名单为空
          this.deletJudgeList = []
          //分页跳转为1
          this.pageInfo.pageNumber = 1
          this.currentPage = 1
          // 重新调用list
          this.getJudgesListByPage()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    //初始化密码
    initCode(id) {
      initPassWord(id).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: '初始化成功',
            type: 'success',
          })
        } else {
          this.$message.error(res.message)
        }
      })
    },
    //确认新增评委
    onSubmit() {
      if (this.form.name == '' || this.form.name.includes(' ')) {
        return this.$message.error('名字不能为空,且不能包含空格')
      } else if (this.form.telephoneNumber == '') {
        return this.$message.error('手机号不能为空')
      } else if (this.form.position == '') {
        return this.$message.error('职位不能为空')
      } else if (this.form.taskId.length == 0) {
        return this.$message.error('任务不能为空')
      }
      addTheJudges(this.courseId, this.form).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: '添加成功',
            type: 'success',
          })
          this.dialog = false
          this.getJudgesListByPage()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    //关闭新增评委
    close() {
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    //搜索评委信息
    searchJudgeInfo() {
      this.getJudgesListByPage()
    },
    //获取审批开关状态
    getApprovalType() {
      getReportTypeTSCP(this.courseId).then((res) => {
        if (res.code == 200) {
          this.value1 = res.data.isComment
        }
      })
    },
    //审批开关按钮
    async changeApproval() {
      examinationApprovalStatus(this.courseId)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.message,
            })
          } else {
            this.getApprovalType()
            this.this.$message.error(res.message)
          }
        })
        .catch((err) => {
          this.getApprovalType()
          this.this.$message.error(err.message)
        })
    },
    // 保存批量导入结果
    saveMoreJudges() {},
    // Excel文件上传
    GetFile($event) {
      let self = this
      let fileType = $event.target.files[0].name
      fileType = fileType.substring(
        fileType.lastIndexOf('.') + 1,
        fileType.length
      )
      if (fileType != 'xls' && fileType != 'xlsx') {
        self.$message.error('请上传Excel文件!')
        return
      }
      self.dialogExcelFileName = $event.target.files[0]
      self.dialogExcelFile = $event.target.files[0].name
    },
    // Excel文件删除
    ExcelDelete() {
      let self = this
      // 清空数据
      self.dialogExcelFile = ''
      $('#excel-file').val('')
    },
    // Excel保存
    ExcelSave() {
      let self = this
      self.currentPage = 1
      self.pagesize = 10
      if (self.dialogExcelFileName == '') {
        self.$message.error('请上传Excel文件!')
        return
      }
      const loading = self.$loading({
        lock: true,
        text: '文件上传中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)',
      })
      let formData = new FormData()
      formData.append('file', self.dialogExcelFileName)

      self.$http
        .post(`/tes/comment/${this.courseId}/importExcel`, formData)
        .then((response) => {
          let res = response.data
          if (res.code == '200') {
            self.$message({
              message: '评委导入成功',
              type: 'success',
            })
            this.getJudgesListByPage()
            self.dialogable = false
            loading.close()
          } else {
            self.$message.error(res.message)
            // 清空数据
            self.dialogExcelFile = ''
            $('#excel-file').val('')
            loading.close()
            self.dialogable = false
          }
        })
        .catch((err) => {
          // self.$message.error('导入失败')
          loading.close()
          self.dialogable = false
        })
    },
  },
}
</script>

<style lang="scss">
#JudgeManagements {
  .el-tooltip__popper {
    max-width: 30%;
    line-height: 22px;
  }
  .chapter-info-table .el-switch__core {
    width: 28px;
    height: 16px;
  }
  .chapter-info-table .el-switch {
    margin-left: 15px;
  }
  .chapter-info-table .el-switch.is-checked .el-switch__core::after {
    margin-left: -13px;
  }
  .chapter-info-table .el-switch__core:after {
    width: 12px;
    height: 12px;
  }
  //新增弹框，提示文字位置调整
  #addjudge {
    .no-ponstion {
      .el-input__prefix,
      .el-input__suffix {
        position: static;
      }
    }
    .el-textarea .el-input__count {
      bottom: 0px;
      right: 80px;
    }
  }
  // 批量导入评委确定键剧中
  .el-dialog__footer {
    text-align: center !important;
  }
}
</style>
<style lang="scss" scoped>
.imgPost {
  vertical-align: inherit;
  position: absolute;
  right: 17px;
  color: rgb(2, 170, 255);
  font-size: 18px;
  padding-top: 5px;
}
.el-button--small:hover {
  color: #1277eb;
}
.el-tree >>> .el-tree-node {
  margin: 6px 0;
}
.el-form-item >>> .el-input__inner {
  width: 200px;
  height: 32px;
  border-radius: 4px;
}
.el-form >>> .el-textarea__inner {
  height: 200px;
  width: 350px;
  resize: none;
}
.el-form-item {
  margin-bottom: 22px;
}
.el-form-item >>> .el-form-item__content {
  height: auto;
}
.block::-webkit-scrollbar {
  width: 2px;
}
.block::-webkit-scrollbar-thumb {
  background: #dededd;
  border-radius: 50px;
}

.chapterTitle {
  width: 390px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  display: inline-block;
}
.custom-tree-node {
  padding-top: 10px;
}
</style>
<style lang="scss" scoped>
@import './../../../styles/ypublic.scss';
.jx-content {
  width: 1200px;
  margin: 20px auto;
  .courseName {
    font-size: 18px;
    display: flex;
    align-items: center;
    .txt {
      width: 94px;
      height: 32px;
    }
    .el-input {
      width: 150px;
    }
    .search-button {
      border: 1px solid #1277eb;
      height: 32px;
      width: 94px;
      text-align: center;
      vertical-align: middle;
      line-height: 30px;
      color: #1277eb;
      cursor: pointer;
      border-radius: 20px;
      padding: 0 16px;
      font-size: 14px;
      display: inline-block;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      margin: 0 15px 0 15px;
      &:hover {
        background-color: #1277eb;
        color: white;
      }
    }
  }
  .totalScore {
    margin-left: 30px;
    font-size: 15px;
  }
  #topic-models {
    padding: 200px 0;
    .button-center {
      text-align: center;
      .button-style {
        text-align: center;
        height: 48px;
        line-height: 48px;
        border-radius: 48px;
        padding: 0 106px;
        margin: 10px;
      }
    }
  }
  .navBox {
    .reportTemplate {
      padding-bottom: 10px;
      margin-left: 190px;
      cursor: pointer;
    }
    .testTemplate {
      padding-bottom: 10px;
      margin-left: 40px;
      cursor: pointer;
    }
    .templateCheck {
      border-bottom: 2px solid $yBlue;
      color: $yBlue;
      cursor: pointer;
    }
  }
  .block {
    margin: 20px 0;
    overflow-y: scroll;
    height: 483px;
  }
  .saveBtn {
    text-align: center;
    margin-top: 10px;
  }
}
.chapter-info-table {
  .table-task {
    width: 100%;
    height: 100%;
    display: flex;
    flex-flow: row wrap;
    .table-task-item {
      width: 20%;
    }
  }
  .table-task-center {
    justify-content: center;
  }
}
//elcel导入
.dialog-excel {
  .excel-btn-box {
    text-align: center;
    margin-top: 15px;
    height: 32px;
    line-height: 32px;
    .button-style {
      position: relative;
      width: 110px;
      margin-right: 20px;
      &:hover {
        span {
          color: $white;
        }
      }
      #excel-file {
        position: absolute;
        width: 100%;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        background: #cc281a;
        opacity: 0;
      }
    }
    span {
      color: #1277eb;
      cursor: pointer;
      i {
        vertical-align: text-top;
        margin-left: 5px;
      }
    }
  }
  P {
    margin-top: 25px;
    text-align: center;
    font-size: 14px;
    color: $title-gray;
    i {
      font-size: 18px;
      color: #385899;
      margin-left: 10px;
    }
  }
}
//excel导入提示
.dialog-excel-tips {
  .el-dialog__body {
    padding: 45px 60px;
  }
  .dialog-content {
    p {
      line-height: 28px;
      span {
        color: #385899;
      }
    }
  }
}
</style>
